package yzserver.sx4a.position;

import com.sitech.portal.system.dao.ISysWorkGroupDao;
import com.sitech.portal.system.domain.TblSYS_WORKGROUP;
import yzserver.sx4a.RetTools;
import yzserver.sx4a.dao.IPositionDao;
import yzserver.sx4a.domain.PositionDomain;
import yzserver.sx4a.form.Position;
import yzserver.sx4a.form.Ret;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: andrew
 * Date: 14-8-15
 * Time: 上午10:14
 * To change this template use File | Settings | File Templates.
 */
public class Sx4aPositionImpl implements Sx4aPosition {

    private IPositionDao positionDao;
    private ISysWorkGroupDao sysWorkGroupDao;

    public void setSysWorkGroupDao(ISysWorkGroupDao sysWorkGroupDao) {
        this.sysWorkGroupDao = sysWorkGroupDao;
    }

    public void setPositionDao(IPositionDao positionDao) {
        this.positionDao = positionDao;
    }

    @Override
    public Position selPosition(String positionid) {
        PositionDomain domain = positionDao.selPosition(positionid);
        if(domain!=null){
            Position position = new Position(domain);
            return position;
        }else{
            return null;
        }

    }

    @Override
    public Ret addPosition(Position position) {
        try {
            positionDao.addPosition(new PositionDomain(position));
        } catch (Exception e) {
            return RetTools.getRet("创建岗位失败", e);
        }

        //portal
        TblSYS_WORKGROUP wk = new TblSYS_WORKGROUP();
        wk.setWORKGROUP_ID(position.getPositionid());
        wk.setWORKGROUP_TYPE(position.getPositionTypeid());
        wk.setWORKGROUP_NAME(position.getPositionname());
        wk.setWORKGROUP_DESC(position.getDisplayname());
        wk.setLOCKSTATE(position.getEnable());
        try {
            sysWorkGroupDao.insertWorkGroup(wk);
        } catch (Exception e) {
            return RetTools.getRet("创建岗位失败", e);
        }
        return RetTools.getRet("创建岗位成功", null);
    }

    @Override
    public Ret updPosition(Position position) {
        try {
            positionDao.updPosition(new PositionDomain(position));
        } catch (Exception e) {
            return RetTools.getRet("修改岗位失败", e);
        }

        //portal
        TblSYS_WORKGROUP wk = new TblSYS_WORKGROUP();
        wk.setWORKGROUP_ID(position.getPositionid());
        wk.setWORKGROUP_TYPE(position.getPositionTypeid());
        wk.setWORKGROUP_NAME(position.getPositionname());
        wk.setWORKGROUP_DESC(position.getDisplayname());
        wk.setLOCKSTATE(position.getEnable());
        try {
            sysWorkGroupDao.updateWorkGroup(wk);
        } catch (Exception e) {
            return RetTools.getRet("修改岗位失败", e);
        }
        return RetTools.getRet("修改岗位成功", null);
    }

    @Override
    public Ret delPosition(String positionid) {

        try {
            positionDao.delPosition(positionid);
        } catch (Exception e) {
            return RetTools.getRet("删除岗位失败", e);
        }
        //portal
        try {
            sysWorkGroupDao.deleteWorkGroup(positionid);
        } catch (Exception e) {
            return RetTools.getRet("删除岗位失败", e);
        }
        return RetTools.getRet("删除岗位成功", null);

    }

    @Override
    public List selAllPosition(String positionid) {
        List<PositionDomain> positionDomains = new ArrayList<PositionDomain>();
        List  positionList = new ArrayList();
        if("all".equalsIgnoreCase(positionid)){ // all为接口约定的固定值
            positionDomains = positionDao.selAllPosition(positionid);
            if(positionDomains!=null){
                for (int i = 0; i <positionDomains.size(); i++) {
                    Position position = new Position(positionDomains.get(i));
                    positionList.add(position);
                }
            }
        }
        return positionList;
    }
}
